//
//  DateMethod.m
//  E-Publishing
//
//  Created by zhangqing on 12-8-1.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "DateMethod.h"

@implementation DateMethod
+(NSString *)timestampFromString:(NSDate *)date{
    NSString *timestampStr;
    NSDate *nowDate = [NSDate date];
    NSTimeInterval timestamp = [nowDate timeIntervalSinceDate:date];
    NSTimeInterval oneDaySecond = 60 * 60 * 24;
    NSTimeInterval oneHourSecond = 60 * 60;
    if (timestamp > oneDaySecond) {
        NSInteger day = timestamp / oneDaySecond;
        timestampStr = [NSString stringWithFormat:@"%lud天前",day];
    }
    
    else if (timestamp > oneHourSecond){
        NSInteger hour = timestamp / oneHourSecond;
        timestampStr = [NSString stringWithFormat:@"%lud小时前",hour];
    }
    else if (timestamp > 60){
        NSInteger minute = timestamp / 60;
        timestampStr = [NSString stringWithFormat:@"%d分钟前",minute];
    }
    else{
        NSInteger sencond = (NSInteger)timestamp % 60;
        timestampStr = [NSString stringWithFormat:@"%d秒前",sencond];
    }
    return timestampStr;

    
}

+(NSDate *)getBeijingDate{
    NSDate* sourceDate = [NSDate date];
    NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"CCT"];
    NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
    NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:sourceDate];
    NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
    NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
    NSDate* destinationDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate];
    return destinationDate;
}

//将日期格式转化为字符串
+ (NSString *)changeDateToString:(NSDate *)date
{
    if (date == nil)
        return nil;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateString = [dateFormatter stringFromDate:date];
   
    
    return dateString;
}

//将字符串转化为日期格式
+ (NSDate *)changeStringToDate:(NSString *)aString
{
    if (aString == nil)
        return nil;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [dateFormatter dateFromString:aString];
   
    
    return date;
}

//将日期格式转化为字符串
+ (NSString *)changeDateTimeToString:(NSDate *)date
{
    if (date == nil)
        return nil;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:date];
   
    
    return dateString;
}

//将日期格式转化为字符串
+ (NSString *)changeDateToMMddTimeToString:(NSDate *)date
{
    if (date == nil)
        return nil;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"MM-dd hh:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:date];
    
    
    return dateString;
}

//将日期格式转为时:分
+ (NSString *)changeDateTohhmmString:(NSDate *)date
{
    if (date == nil)
        return nil;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:mm"];
    NSString *dateString = [dateFormatter stringFromDate:date];
    
    
    return dateString;
}

//将日期格式转化为字符串
+ (NSString *)changeDateTimeToStringTimeFirst:(NSDate *)date
{
    if (date == nil)
        return nil;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"hh:mm:ss yyyy-MM-dd"];
    NSString *dateString = [dateFormatter stringFromDate:date];
    
    return dateString;
}

//将字符串转化为日期格式
+ (NSDate *)changeStringToDateTime:(NSString *)aString
{
    if (aString == nil)
        return nil;
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"];
    NSDate *date = [dateFormatter dateFromString:aString];
   
    
    return date;
}

//将一个日期转换成今年的月-日 格式信息
+ (NSDate *)changeSignalDateToNowYear:(NSDate *)date
{
    if (date == nil)
        return nil;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateStr = [formatter stringFromDate:date];
    NSString *nowDate = [formatter stringFromDate:[NSDate date]];
    NSArray *nowDateArray = [nowDate componentsSeparatedByString:@"-"];
    NSArray *dateStrArray = [dateStr componentsSeparatedByString:@"-"];
    NSString *resultStr = [NSString stringWithFormat:@"%@-%@-%@", 
                           [nowDateArray objectAtIndex:0],
                           [dateStrArray objectAtIndex:1],
                           [dateStrArray objectAtIndex:2]];
    NSLog(@"转换成今年的生日日期为:%@", resultStr);
    NSDate *result = [formatter dateFromString:resultStr];
    return result;
}

//获得当前时间的年月
+ (NSString *)getCurrentYearAndMonthString
{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateStr = [formatter stringFromDate:[NSDate date]];
    NSArray *tempArr = [dateStr componentsSeparatedByString:@"-"];
    NSString *nowMonth = [NSString stringWithFormat:@"%@-%@", [tempArr objectAtIndex:0], [tempArr objectAtIndex:1]];
    return nowMonth;
}

//将日期转换成中文表达方式
+ (NSString *)changeDateToChiniseMonthDayString:(NSDate *)date
{
    if (date == nil)
        return nil;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateStr = [formatter stringFromDate:date];
    //NSString *nowDate = [formatter stringFromDate:[NSDate date]];
    NSArray *dateStrArray = [dateStr componentsSeparatedByString:@"-"];
    NSString *resultStr = [NSString stringWithFormat:@"%@月%@日", 
                           [dateStrArray objectAtIndex:1],
                           [dateStrArray objectAtIndex:2]];
    return resultStr;
}

//将秒数转换成电子表形式时间
+ (NSString *)changeTimeToDigitalTimeString:(double)dtime
{
    dtime = dtime /1000;
    int iHour = dtime / 3600; 
    int iMinuten = (dtime-iHour * 3600) / 60; 
    int iSencond = dtime-iHour * 3600 - iMinuten * 60;
    
    return [NSString stringWithFormat:@"%02d:%02d:%02d", iHour, iMinuten, iSencond];
}

//将时间戳转化为时分秒
+ (NSString *)changeTimeToHourMinSec:(double)dtime{
     NSDate *date = [NSDate dateWithTimeIntervalSince1970:dtime];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    //设定时间格式,这里可以设置成自己需要的格式
    [dateFormatter setDateFormat:@"HH:mm:ss"];
    //用[NSDate date]可以获取系统当前时间
    NSString *currentDateStr = [dateFormatter stringFromDate:date];
    //输出格式为：2010-10-27 10:22:13
    NSLog(@"%@",currentDateStr);
    //alloc后对不使用的对象别忘了release
       return currentDateStr;
}
//将秒数转换成年、月、日
+ (NSDate *)changeTimeToDate:(double)dtime{
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:dtime];
    NSTimeInterval  timeZoneOffset=[[NSTimeZone systemTimeZone] secondsFromGMT];
    date = [date dateByAddingTimeInterval:timeZoneOffset];
    
    return date;
}

+ (int )backWeekNumWithTimes:(double)dtime{
	NSDate *chooseDate=[NSDate dateWithTimeIntervalSince1970:dtime];
    NSDateFormatter *weekFor = [[NSDateFormatter alloc] init];
    [weekFor setDateFormat:@"EEE"];
    NSString *weekNum=[weekFor stringFromDate:chooseDate];
    if ([weekNum isEqualToString:@"周一"]||[weekNum isEqualToString:@"Mon"]) {
		return 1;
    }else if ([weekNum isEqualToString:@"周二"]||[weekNum isEqualToString:@"Tue"]) {
		return 2;
    }else if ([weekNum isEqualToString:@"周三"]||[weekNum isEqualToString:@"Wed"]) {
		return 3;
    }else  if ([weekNum isEqualToString:@"周四"]||[weekNum isEqualToString:@"Thu"]) {
		return 4;
    }else if ([weekNum isEqualToString:@"周五"]||[weekNum isEqualToString:@"Fri"]) {
		return 5;
    }else if ([weekNum isEqualToString:@"周六"]||[weekNum isEqualToString:@"Sat"]) {
		return 6;
    }else if ([weekNum isEqualToString:@"周日"]||[weekNum isEqualToString:@"Sun"]) {
		return 7;
	}

	return 1;
	
}
+ (double)backDayZeroTimeWithDate:(NSDate*)_date{
	double timeTaps_0 ;
	NSDateFormatter *yearFor = [[NSDateFormatter alloc] init ];
    [yearFor setDateFormat:@"yyyy"];
    NSString *yearNum=[yearFor stringFromDate:_date];
    NSDateFormatter *mouthFor = [[NSDateFormatter alloc] init ];
    [mouthFor setDateFormat:@"MM"];
    NSString *mouthNum=[mouthFor stringFromDate:_date];
    NSDateFormatter *dayFor = [[NSDateFormatter alloc] init ];
    [dayFor setDateFormat:@"dd"];
    NSString *dayNum=[dayFor stringFromDate:_date];
	
    NSString* timeStr = [NSString stringWithFormat:@"%@-%@-%@ 00:00:00",yearNum,mouthNum,dayNum] ;
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate* date_0 = [formatter dateFromString:timeStr];
    timeTaps_0 = [date_0 timeIntervalSince1970];
	return timeTaps_0;
	
}
//比较选择的时间是否大于当前时间
+ (BOOL)compareDateWithChooseTimes:(double)dtime{
	NSDate *date=[NSDate date];
	double chooseTimes=dtime;
	double nowTimes=[date timeIntervalSince1970];
	
	if (chooseTimes >nowTimes) {
		return YES;
	}else {
		return NO;
	}
}
//比较选择的时间戳是否大于当前时间
+ (BOOL)compareDateWithChooseDate:(NSDate *)_date{
    NSDate *date=[NSDate date];
    if ([_date timeIntervalSince1970] < [date timeIntervalSince1970]) {
        return YES;
    }
    return NO;

}

//返回当前年份
+ (int )yearWithDate:(NSDate *)_date{
    NSDateFormatter *yearFor = [[NSDateFormatter alloc] init];
    [yearFor setDateFormat:@"yyyy"];
    NSString *yearNum=[yearFor stringFromDate:_date];
    int year=[yearNum intValue];
    return year;
}

@end
